package eg.edu.eelu.fyp2013.jdetector.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JSplitPane;
import javax.swing.JToolBar;
import javax.swing.border.EmptyBorder;

public class HistogramFrame extends JFrame {
	private static final long serialVersionUID = 1L;

	private JPanel contentPane;
	public HistogramGraphPanel leftHistogramGraphPanel;
	public HistogramGraphPanel rightHistogramGraphPanel;
	private JMenuBar menuBar;
	private JToolBar toolBar;
	private float zoomFactor=1;
	private JButton zoomOutButton;
	private JButton zoomInButton;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					HistogramFrame frame = new HistogramFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public HistogramFrame() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		
		menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JSplitPane splitPane = new JSplitPane();
		
		leftHistogramGraphPanel = new HistogramGraphPanel();
		splitPane.setLeftComponent(leftHistogramGraphPanel);

		rightHistogramGraphPanel = new HistogramGraphPanel();
		splitPane.setRightComponent(rightHistogramGraphPanel);

		splitPane.addComponentListener(new ComponentAdapter() {
			@Override
			public void componentResized(ComponentEvent e) {
				((JSplitPane)e.getSource()).setDividerLocation(0.5);
			}
		});
		contentPane.add(splitPane, BorderLayout.CENTER);
		
		toolBar = new JToolBar();
		contentPane.add(toolBar, BorderLayout.NORTH);
		
		zoomOutButton = new JButton("( - )");
		zoomOutButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				zoomFactor/=2;
				leftHistogramGraphPanel.setZoomFactor(zoomFactor);
				rightHistogramGraphPanel.setZoomFactor(zoomFactor);
			}
		});
		toolBar.add(zoomOutButton);
		
		zoomInButton = new JButton("( + )");
		zoomInButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				zoomFactor*=2;
				leftHistogramGraphPanel.setZoomFactor(zoomFactor);
				rightHistogramGraphPanel.setZoomFactor(zoomFactor);
			}
		});
		toolBar.add(zoomInButton);
	}

}
